package com.ruoyi.system.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import com.ruoyi.system.dl.DeleteImages;
import com.ruoyi.system.domain.Images;
import com.ruoyi.system.mapper.ImagesMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.HotelMapper;
import com.ruoyi.system.domain.Hotel;
import com.ruoyi.system.service.IHotelService;

/**
 * 酒店Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@Service
public class HotelServiceImpl implements IHotelService 
{
    @Autowired
    private HotelMapper hotelMapper;

    @Autowired
    private ImagesMapper imagesMapper;

    /**
     * 查询酒店
     * 
     * @param id 酒店主键
     * @return 酒店
     */
    @Override
    public Hotel selectHotelById(Long id) {
        Hotel hotel = hotelMapper.selectHotelById(id);
        hotel.setHotelImgs(imagesMapper.selectImages("hotel"+id));
        return hotel;
    }

    /**
     * 查询酒店列表
     * 
     * @param hotel 酒店
     * @return 酒店
     */
    @Override
    public List<Hotel> selectHotelList(Hotel hotel) {
        List<Hotel> hotels = hotelMapper.selectHotelList(hotel);

        return hotels.stream().peek(hot -> {
            hot.setHotelImgs(imagesMapper.selectImages("hotel"+hot.getId()));
        }).collect(Collectors.toList());
    }

    /**
     * 新增酒店
     * 
     * @param hotel 酒店
     * @return 结果
     */
    @Override
    public int insertHotel(Hotel hotel) {
       int i = hotelMapper.insertHotel(hotel);

       // 里面只有一个对象
        List<Hotel> hotels = hotelMapper.selectHotelList(hotel);
        Long hotelId = hotels.get(0).getId();

        List<String> hotelImgs = hotel.getHotelImgs();

        hotelImgs.forEach(o->imagesMapper.insertSelective(new Images(null,"hotel"+hotelId,o)));

        return i;
    }

    /**
     * 修改酒店
     * 
     * @param hotel 酒店
     * @return 结果
     */
    @Override
    public int updateHotel(Hotel hotel) {
        int i = hotelMapper.updateHotel(hotel);

        String foreignId = "hotel" + hotel.getId();
        DeleteImages.deleteIma(foreignId,imagesMapper);
        // 将所有与该id的图片删除
        imagesMapper.deleteByForeginId(foreignId);

        Set<String> strings = new HashSet<>(hotel.getHotelImgs());

        // 添加
        strings.forEach(o -> imagesMapper.insert(new Images(null,foreignId,o)));

        return i;
    }

    /**
     * 批量删除酒店
     * 
     * @param ids 需要删除的酒店主键
     * @return 结果
     */
    @Override
    public int deleteHotelByIds(Long[] ids) {
        Arrays.stream(ids).forEach(id -> {
            imagesMapper.deleteByForeginId("hotel"+id);
            DeleteImages.deleteIma("hotel"+id,imagesMapper);
        });

        return hotelMapper.deleteHotelByIds(ids);
    }

    /**
     * 删除酒店信息
     * 
     * @param id 酒店主键
     * @return 结果
     */
    @Override
    public int deleteHotelById(Long id) {
        imagesMapper.deleteByForeginId("hotel"+id);
        DeleteImages.deleteIma("hotel"+id,imagesMapper);
        return hotelMapper.deleteHotelById(id);
    }
}
